package com.hnou.boxuegu.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.hnou.boxuegu.R;
import com.hnou.boxuegu.adapter.CourseDetailAdapter;
import com.hnou.boxuegu.bean.CourseBean;
import com.hnou.boxuegu.bean.VideoBean;
import com.hnou.boxuegu.utils.DBUtils;
import com.hnou.boxuegu.utils.UtilsHelper;

import java.util.List;

public class CourseDetailActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv_intro,tv_video,tv_chapter_intro;
    private RecyclerView rv_list;
    private ScrollView sv_chapter_intro;
    private CourseDetailAdapter adapter;
    private List<VideoBean> videoList;
    private String chapterName,intro;
    private DBUtils db;
    private CourseBean bean;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);
        bean = (CourseBean) getIntent().getSerializableExtra("CourseBean");
        id = bean.getId();
        chapterName = bean.getChapterName();
        intro = bean.getChapterIntro();
        videoList = bean.getVideoList();
        db = DBUtils.getInstance(CourseDetailActivity.this);
        init();
    }

    private void init() {

        tv_intro = findViewById(R.id.tv_intro);
        tv_video = findViewById(R.id.tv_video);
        rv_list = findViewById(R.id.rv_list);
        tv_chapter_intro = findViewById(R.id.tv_chapter_intro);
        sv_chapter_intro = findViewById(R.id.sv_chapter_intro);
        tv_intro.setOnClickListener(this);
        tv_video.setOnClickListener(this);
        adapter = new CourseDetailAdapter(this, new CourseDetailAdapter.OnSelectListener() {
            @Override
            public void onSelect(int position, ImageView iv) {
                adapter.setSelectedPosition(position);
                VideoBean bean = videoList.get(position);
                String videoPath = bean.getVideoPath();
                adapter.notifyDataSetChanged();
                if(TextUtils.isEmpty(videoPath)){
                    Toast.makeText(CourseDetailActivity.this,"本地没有此视频，暂无法播放",Toast.LENGTH_SHORT).show();
                    return;

                }else {
                    if(UtilsHelper.readLoginStatus(
                            CourseDetailActivity.this
                    )){
                        String userName = UtilsHelper.readLoginUserName(CourseDetailActivity.this);
                        db.saveVideoPlayList(id,chapterName,videoList.get(position),userName);
                    }
                    Intent intent=new Intent(CourseDetailActivity.this,VideoPlayActivity.class);
                    intent.putExtra("videoPath",videoPath);
                    startActivityForResult(intent,1);
                }
            }
        });
        rv_list.setLayoutManager(new LinearLayoutManager(this));
        rv_list.setAdapter(adapter);
        adapter.setDate(videoList);
        tv_chapter_intro.setText(intro);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_intro:
                rv_list.setVisibility(View.GONE);
                sv_chapter_intro.setVisibility(View.VISIBLE);
                tv_intro.setBackgroundResource(R.drawable.video_list_intro_blue);
                tv_video.setBackgroundResource(R.drawable.video_list_intro_white);
                tv_intro.setTextColor(Color.parseColor("#FFFFFF"));
                tv_video.setTextColor(Color.parseColor("#000000"));
                break;
            case R.id.tv_video:
                rv_list.setVisibility(View.VISIBLE);
                sv_chapter_intro.setVisibility(View.GONE);
                tv_intro.setBackgroundResource(R.drawable.video_list_intro_white);
                tv_video.setBackgroundResource(R.drawable.video_list_intro_blue);
                tv_intro.setTextColor(Color.parseColor("#FFFFFF"));
                tv_video.setTextColor(Color.parseColor("#000000"));
                break;
            default:
                break;
        }
    }
}